package servlet;

import beans.ModelBean;
import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

import javax.servlet.*;
import javax.servlet.http.*;

public class ChangeModelServlet extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String id="";
        int itemID = 0;
        String name="";
        String type="";
        String company="";
        String scale="";
        String price="";
        String quantity="";
        String description=null;
        String distributionDate = "";
        String fileName1=null;
        String imageURL = null;
        String image1 = "", image2 = "", image3 = "", image4 = "";
        String imageID1 = "", imageID2 = "", imageID3 = "", imageID4 = "";
        boolean ok = true;
        
        try{
            DiskFileUpload fu = new DiskFileUpload();
            
            fu.setSizeThreshold(4096);
            fu.setSizeMax(1000000);
            //fu.setRepositoryPath(application.getRealPath("/") + "images/");
            //out.println(fu.getRepositoryPath());
            
            List fileItems = fu.parseRequest(request);
            
            Iterator itr = fileItems.iterator();
            //ArrayList<String> image = new ArrayList<String>();
            String[] finalImage = new String[0];
            while(itr.hasNext()) {
                FileItem fi = (FileItem)itr.next();
                if(fi.isFormField()) {
                    String fieldName = fi.getFieldName();
                    out.println(fi.getString() + "<br>");
                    if (fieldName.equals("itemID")) {
                        id = fi.getString();
                        itemID = Integer.parseInt(id);
                        out.println("ItemID: " +id+ "<br>");
                    }else if (fieldName.equals("name")) {
                        name = fi.getString();
                        out.println("Name: " +name+ "<br>");
                    }else if (fieldName.equals("type")) {
                        type = fi.getString();
                        out.println("Type: " +type+ "<br>");
                    }else if (fieldName.equals("company")) {
                        company = fi.getString();
                        out.println("Company: " +company+ "<br>");
                    }else if (fieldName.equals("scale")) {
                        scale = fi.getString();
                        out.println("Scale: " +scale+ "<br>");
                    }else if (fieldName.equals("price")) {
                        price = fi.getString();
                        out.println("Price: " +price+ "<br>");
                    }else if (fieldName.equals("quantity")) {
                        quantity = fi.getString();
                        out.println("Quantity: " +quantity+ "<br>");
                    }else if (fieldName.equals("description")) {
                        description = fi.getString();
                    }else if (fieldName.equals("distributionDate")) {
                        distributionDate = fi.getString();
                    }else if (fieldName.equals("imageURL")) {
                        imageURL = fi.getString();
                    }else if (fieldName.equals("imageID1")) {
                        imageID1 = fi.getString();
                    }else if (fieldName.equals("imageID2")) {
                        imageID2 = fi.getString();
                    } else if (fieldName.equals("imageID3")) {
                        imageID3 = fi.getString();
                    } else if (fieldName.equals("imageID4")) {
                        imageID4 = fi.getString();
                    }else {
                        out.println("Error occurs");
                    }
                    
                }else {
                    String fieldName = fi.getFieldName();
                    if(fieldName.equals("image1")){
                        if(!"".equals(fi.getName())) {
                            image1 = fi.getName();
                            out.println(fi.getName());
                            out.println("SIZE: "+fi.getSize());
                            ModelBean.uploadImage(fi);
                        }
                    }else if(fieldName.equals("image2")){
                        if(!"".equals(fi.getName())) {
                            image2 = fi.getName();
                            out.println(fi.getName());
                            out.println("SIZE: "+fi.getSize());
                            ModelBean.uploadImage(fi);
                        }
                    }else if(fieldName.equals("image3")){
                        if(!"".equals(fi.getName())) {
                            image3 = fi.getName();
                            out.println(fi.getName());
                            out.println("SIZE: "+fi.getSize());
                            ModelBean.uploadImage(fi);
                        }
                    }else if(fieldName.equals("image4")){
                        if(!"".equals(fi.getName())) {
                            image4 = fi.getName();
                            out.println(fi.getName());
                            out.println("SIZE: "+fi.getSize());
                            ModelBean.uploadImage(fi);
                        }
                    }else if(fieldName.equals("image")){
                        if(!"".equals(fi.getName())) {
                            out.println(fi.getName());
                            out.println("SIZE: "+fi.getSize());
                            fileName1 = ModelBean.uploadImage(fi);
                        }
                    }else {
                        out.println("Error occurs");
                    }
                }//end else
            }//end while
            if (ok) {
                if ("".equals(fileName1) ) {
                    if ("".equals(imageURL)) {
                        imageURL = (String)request.getSession().getAttribute("imageURL");
                    }
                    fileName1 = imageURL;
                }
                int result = ModelBean.updateModel(quantity, name, type, company, scale, price, distributionDate, fileName1, description, itemID);
                if(result == -1) {
                    
                }
                
                if (!"".equals(image1)) {
                    ModelBean.updateImageLink(imageID1, image1);
                }
                if (!"".equals(image2)) {
                    ModelBean.updateImageLink(imageID2, image2);
                }
                if (!"".equals(image3)) {
                    ModelBean.updateImageLink(imageID3, image3);
                }
                if (!"".equals(image4)) {
                    ModelBean.updateImageLink(imageID4, image4);
                }
                request.getSession().setAttribute("complete_msg", "Update Stock Information Complete");
                //response.sendRedirect("admin/completePage.jsp");
                return;
            }
        }catch (Exception e){
            //response.sendRedirect("ErrorPage.jsp");
            return;
        }
        
        
    }
    
    
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
